#include <windows.h>
#include "WindowManager.h"
#include "KeyboardHook.h"
#include "DataManager.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);
    UNREFERENCED_PARAMETER(nCmdShow);
    // Initialize window manager
    WindowManager windowManager;
    
    if (!windowManager.Initialize()) {
        return -1;
    }

    // Create main window
    windowManager.CreateMainWindow();
    if (!windowManager.GetWindowHandle()) {
        return -1;
    }

    // Create and install keyboard hook
    KeyboardHook keyboardHook;
    
    // Set the data manager for the keyboard hook
    keyboardHook.SetDataManager(windowManager.GetDataManager());
    
    // Set up keyboard click callback
    keyboardHook.SetClickCallback([&windowManager](int count) {
        windowManager.UpdateClickCount(count);
        windowManager.SetNekoState(true);  // Activate neko, will auto-reset after 0.15s
    });
    
    // Install the keyboard hook
    if (!keyboardHook.InstallHook()) {
        return -1;
    }

    // Create system tray icon
    if (!windowManager.CreateSystemTray()) {
        return -1;
    }

    // Set up reset callback
    windowManager.SetResetCallback([&windowManager]() {
        if (windowManager.GetDataManager()) {
            windowManager.GetDataManager()->ResetTodayCount();
        }
    });

    // Show the window
    windowManager.ShowWindow();

    // Message loop
    MSG msg = {};
    while (GetMessage(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    // Cleanup
    keyboardHook.UninstallHook();
    
    return 0;
}
